package com.lineage.server.model;

import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_Paralysis;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.thread.GeneralThreadPool;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1CurseParalysis extends L1Paralysis {
  private static final Log _log = LogFactory.getLog(L1CurseParalysis.class);
  
  private final L1Character _target;
  
  private final int _delay;
  
  private final int _time;
  
  private Thread _timer;
  
  private L1CurseParalysis(L1Character cha, int delay, int time, int mode) {
    this._target = cha;
    this._delay = delay;
    this._time = time;
    curse(mode);
  }
  
  private void curse(int mode) {
    if (this._target instanceof L1PcInstance) {
      L1PcInstance player = (L1PcInstance)this._target;
      switch (mode) {
        case 1:
          player.sendPackets((ServerBasePacket)new S_ServerMessage(212));
          break;
        case 2:
          player.sendPackets((ServerBasePacket)new S_ServerMessage(291));
          break;
      } 
    } 
    this._target.setPoisonEffect(2);
    this._timer = new ParalysisDelayTimer();
    GeneralThreadPool.get().execute(this._timer);
  }
  
  public static boolean curse(L1Character cha, int delay, int time, int mode) {
    if (!(cha instanceof L1PcInstance) && !(cha instanceof com.lineage.server.model.Instance.L1MonsterInstance))
      return false; 
    if (cha.hasSkillEffect(1010) || cha.hasSkillEffect(1011))
      return false; 
    cha.setParalaysis(new L1CurseParalysis(cha, delay, time, mode));
    return true;
  }
  
  public int getEffectId() {
    return 2;
  }
  
  public void cure() {
    this._target.setPoisonEffect(0);
    this._target.setParalaysis(null);
    if (this._timer != null)
      this._timer.interrupt(); 
  }
  
  private class ParalysisDelayTimer extends Thread {
    private ParalysisDelayTimer() {}
    
    public void run() {
      L1CurseParalysis.this._target.setSkillEffect(1010, 0);
      try {
        Thread.sleep(L1CurseParalysis.this._delay);
      } catch (InterruptedException e) {
        L1CurseParalysis.this._target.killSkillEffectTimer(1010);
        ModelError.isError(L1CurseParalysis._log, e.getLocalizedMessage(), e);
        return;
      } 
      if (L1CurseParalysis.this._target instanceof L1PcInstance) {
        L1PcInstance player = (L1PcInstance)L1CurseParalysis.this._target;
        if (!player.isDead())
          player.sendPackets((ServerBasePacket)new S_Paralysis(1, true)); 
      } 
      L1CurseParalysis.this._target.setParalyzed(true);
      L1CurseParalysis.this._timer = new L1CurseParalysis.ParalysisTimer();
      GeneralThreadPool.get().execute(L1CurseParalysis.this._timer);
      if (isInterrupted())
        L1CurseParalysis.this._timer.interrupt(); 
    }
  }
  
  private class ParalysisTimer extends Thread {
    private ParalysisTimer() {}
    
    public void run() {
      L1CurseParalysis.this._target.killSkillEffectTimer(1010);
      L1CurseParalysis.this._target.setSkillEffect(1011, 0);
      try {
        Thread.sleep(L1CurseParalysis.this._time);
      } catch (InterruptedException e) {
        ModelError.isError(L1CurseParalysis._log, e.getLocalizedMessage(), e);
      } 
      L1CurseParalysis.this._target.killSkillEffectTimer(1011);
      if (L1CurseParalysis.this._target instanceof L1PcInstance) {
        L1PcInstance player = (L1PcInstance)L1CurseParalysis.this._target;
        if (!player.isDead())
          player.sendPackets((ServerBasePacket)new S_Paralysis(1, false)); 
      } 
      L1CurseParalysis.this._target.setParalyzed(false);
      L1CurseParalysis.this.cure();
    }
  }
}
